import { Component, OnInit , Input , Output , EventEmitter } from '@angular/core';
import * as $ from 'jquery';
@Component({
  selector: 'app-fenye',
  templateUrl: './fenye.component.html',
  styleUrls: ['./fenye.component.css']
})
export class FenyeComponent implements OnInit {
  @Input() dataSum: number;
  @Output() private outer = new EventEmitter<string>();
  private dot_num = 1;       // 分页排列从1开始
  private page_sum: any;    // 页的总数
  private  Pointer_index: any = 1; // 当前页数
  private jumptoVale: any;
  private click_box = [0, 1, 2, 3, 4];
  private listdata = 10;     // 排列10条数据
  private index_sum = 5;     // 5个点击的指针

  constructor() { }

  ngOnInit() {
    this.index(this.dataSum);
  }
  index(num_sum) {
    this.dot_num = 1;
    this.Pointer_index = 1;
    this.page_sum = Math.ceil( num_sum / this.listdata); // 计算多小页
    let index = 0 ;
    if ( this.page_sum < 5) {
      index = this.page_sum ;
    } else {
      index = 0 ;
    }
    switch (index) {
      case 0:
        this.click_box = [0, 1, 2, 3, 4];
        break;
      case 1:
        this.click_box = [0];
        break;
      case 2:
        this.click_box = [0, 1];
        break;
      case 3:
        this.click_box = [0, 1, 2];
        break;
      case 4:
        this.click_box = [0, 1, 2, 3];
        break;
      default:
    }
  }
  next(click_box) { // 下一页
    if ( this.Pointer_index % this.index_sum == 0 && this.Pointer_index < this.page_sum) {
      this.dot_num += 5;
    }
    if (this.Pointer_index >= (this.page_sum - this.page_sum % 5)) { // 判断最后一页 隐藏没的按钮
      let index = this.page_sum % 5;  // 数据后面删除index个
      if ( this.page_sum < 5) {
        index = this.page_sum ;
      }
      switch (index) {
        case 1:
          this.click_box = [0];
          break;
        case 2:
          this.click_box = [0, 1];
          break;
        case 3:
          this.click_box = [0, 1, 2];
          break;
        case 4:
          this.click_box = [0, 1, 2, 3];
          break;
        default:
      }
    }
    if (this.Pointer_index < this.page_sum) {
      this.Pointer_index++;
    } else {
      this.Pointer_index = this.page_sum;
    }
    this.outer.emit(this.Pointer_index);

  }
  pre(click_box) { // 上一页
    if (this.Pointer_index > 1) {
      this.Pointer_index--;
    } else {
      this.Pointer_index = 1;
    }
    if (this.Pointer_index > (this.page_sum - this.page_sum % 5)) { // 判断最后一页
      const index = this.page_sum % 5 ;
      this.click_box.splice(index , 5);
    } else {
      this.click_box = [0, 1, 2, 3, 4];
    }
    if (this.Pointer_index % this.index_sum == 0 && this.Pointer_index > 0) {
      this.dot_num -= 5;
    }
    this.outer.emit(this.Pointer_index);

  }
  one_page() {
    this.dot_num = 1;
    this.Pointer_index = 1;
    this.index(this.dataSum);
    this.outer.emit(this.Pointer_index);
  }
  last_page() {
    this.Pointer_index = this.page_sum;
    const index = this.page_sum % 5;  // 数据后面删除index个
    switch (index) {
      case 0:
        this.click_box = [0, 1, 2, 3, 4];
        this.dot_num = this.page_sum - 4;
        break;
      case 1:
        this.click_box = [0];
        this.dot_num = this.page_sum ;
        break;
      case 2:
        this.click_box = [0, 1];
        this.dot_num = this.page_sum - 1;
        break;
      case 3:
        this.click_box = [0, 1, 2];
        this.dot_num = this.page_sum - 2;
        break;
      case 4:
        this.click_box = [0, 1, 2, 3];
        this.dot_num = this.page_sum - 3;
        break;
      default:
    }
    this.outer.emit(this.Pointer_index);
  }
  jumpto() {
    const num = /^\+?[1-9][0-9]*$/;
    if (num.test(this.jumptoVale) && this.jumptoVale <= this.page_sum) {
      this.Pointer_index = this.jumptoVale;
      if (this.Pointer_index > ( this.page_sum - this.page_sum % 5)) {
        const index = this.page_sum % 5;
        switch (index) {
          case 1:
            this.click_box = [0];
            break;
          case 2:
            this.click_box = [0, 1];
            break;
          case 3:
            this.click_box = [0, 1, 2];
            break;
          case 4:
            this.click_box = [0, 1, 2, 3];
            break;
          default:
        }
      } else {
        this.click_box = [0, 1, 2, 3, 4];
      }
      const j =  parseInt(this.jumptoVale);
      if (j % 5) {
        this.dot_num = j - j % 5 + 1;
      } else {
        this.dot_num = j - 4;
      }
    } else {
      alert('请输入合适页数');
    }
    this.outer.emit(this.Pointer_index);
  }
  click_boxs(key) {  // 选中那几页
    this.Pointer_index = this.dot_num + key;
    this.outer.emit(this.Pointer_index);
  }
}
